package com.yzjs.localhost.service.tNeusoftUniformityTesting.impl;

import com.alibaba.fastjson.JSONObject;
import com.yzjs.common.base.service.BaseServiceImpl;
import com.yzjs.common.util.conversion.JsonUtil;
import com.yzjs.common.util.httpclient.HttpUtils;
import com.yzjs.localhost.dictionaries.DealDictionary;
import com.yzjs.localhost.dto.AlertPushDTO;
import com.yzjs.localhost.dto.TNeusoftUniformityTestingDTO;
import com.yzjs.localhost.entity.tDataPersistenceAfter.TDataPersistenceAfterEntity;
import com.yzjs.localhost.entity.tDataPersistenceBeforce.TDataPersistenceBeforceEntity;
import com.yzjs.localhost.entity.tNeusoftComplianceTesting.TNeusoftComplianceTestingEntity;
import com.yzjs.localhost.entity.tNeusoftUniformityTesting.TNeusoftUniformityTestingEntity;
import com.yzjs.localhost.dao.tNeusoftUniformityTesting.TNeusoftUniformityTestingMapper;
import com.yzjs.localhost.service.AnalysisService;
import com.yzjs.localhost.service.tDataPersistenceAfter.TDataPersistenceAfterService;
import com.yzjs.localhost.service.tDataPersistenceBeforce.TDataPersistenceBeforceService;
import com.yzjs.localhost.service.tNeusoftComplianceTesting.impl.TNeusoftComplianceTestingServiceImpl;
import com.yzjs.localhost.service.tNeusoftUniformityTesting.TNeusoftUniformityTestingService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;


@Service("tNeusoftUniformityTestingService")
public class TNeusoftUniformityTestingServiceImpl extends BaseServiceImpl<TNeusoftUniformityTestingEntity, TNeusoftUniformityTestingMapper > implements TNeusoftUniformityTestingService {


    @Value("${bdms.url}")
    private String  bdmsUrl ;

    @Value("${doong.push.url}")
    private String doongPushUrl ;

    private static Logger logger = LogManager.getLogger(TNeusoftComplianceTestingServiceImpl.class.getName());

    @Autowired
    private TDataPersistenceBeforceService dataPersistenceBeforceService;

    @Autowired
    private TDataPersistenceAfterService  dataPersistenceAfterService;

    @Override
    public void deal(TNeusoftUniformityTestingEntity tNeusoftUniformityTesting) throws Exception {
        if(DealDictionary.PROCESSED ==tNeusoftUniformityTesting.getDisposalStatus() ){
            TNeusoftUniformityTestingEntity query = new TNeusoftUniformityTestingEntity();
            query.setId(tNeusoftUniformityTesting.getId());
            TNeusoftUniformityTestingEntity result = this.findOne(query);

            String restJson = "operationId="+result.getOperationId()+",jurisdictionCode="+result.getJurisdictionCode()+
                    ",warningInfo"+result.getWarningInfo();
            String url = doongPushUrl+"/auth/callback/notify?appSecret=n9wHjD9lJwdEP3YFnHTH7C814f6E4YgTDhG41iUZJsKfWVYCTxnMyyhE05d&restJson="+URLEncoder.encode(restJson, "UTF-8");
            logger.info(url);
            // 调用东软服务接口
            String resultOut  = HttpUtils.post(url,new HashMap<>(),5000);
            logger.info("---------------------tNeusoftUniformityTesting 返回结果信息 "+resultOut);
        }

        this.update(tNeusoftUniformityTesting);
    }

    @Override
    public TNeusoftUniformityTestingDTO getDetail(TNeusoftUniformityTestingEntity query) throws Exception {
        TNeusoftUniformityTestingDTO  dto = new TNeusoftUniformityTestingDTO();
        TNeusoftUniformityTestingEntity  uniformityTesting  = this.findOne(query);
        dto.setNeusoftUniformityTesting(uniformityTesting);

        // 获取入参前信息
        TDataPersistenceBeforceEntity beforceEntity  = new TDataPersistenceBeforceEntity();
        beforceEntity.setDataIdentification(uniformityTesting.getDataIdentification());
        List<TDataPersistenceBeforceEntity> beforceList =dataPersistenceBeforceService.findList(beforceEntity);
        if(beforceList != null && beforceList.size()>0){
            Map<String,Object> treeMap  = new TreeMap<>();
            TDataPersistenceBeforceEntity  after= beforceList.get(0);
            Map<String,Object> infoMap = JsonUtil.json2map(after.getInfo());
            for(Map.Entry<String,Object> entry:infoMap.entrySet()){
                String key = AnalysisService.translationMap.get(entry.getKey());
                if ( key== null) {
                    key = entry.getKey();
                }
                treeMap.put(key,entry.getValue());

            }
            after.setInfo(JsonUtil.mapToJsonIgnoreNull(treeMap));
            dto.setBeforceEntity(beforceList.get(0));
        }

        // 获取入参后的信息
        TDataPersistenceAfterEntity  afterEntity  = new TDataPersistenceAfterEntity();
        afterEntity.setDataIdentification(uniformityTesting.getDataIdentification());
        List<TDataPersistenceAfterEntity> afterList = dataPersistenceAfterService.findList(afterEntity);

        if(afterList != null && afterList.size()>0){
            Map<String,Object> treeMap  = new TreeMap<>();
            TDataPersistenceAfterEntity  after= afterList.get(0);
            Map<String,Object> infoMap = JsonUtil.json2map(after.getInfo());
            for(Map.Entry<String,Object> entry:infoMap.entrySet()){
                String key = AnalysisService.translationMap.get(entry.getKey());
                if ( key== null) {
                    key = entry.getKey();
                }
                treeMap.put(key,entry.getValue());

            }
            after.setInfo(JsonUtil.mapToJsonIgnoreNull(treeMap));
            dto.setAfterEntity(afterList.get(0));
        }

        //获取区块链链码信息
        String dbUrl = bdmsUrl+"/v1/datasourcesettings/list?table_name=t_neusoft_uniformity_testing";
        String result= HttpUtils.get(dbUrl,5000);
        Map<String,Object> tableMap = JsonUtil.json2map(result);
        List<Map<String,Object>> list = (List<Map<String, Object>>) tableMap.get("data");
        if(list != null &&list.size()>0){
            Integer dsId = (Integer) list.get(0).get("id");
            String tableName = (String) list.get(0).get("table_name");
            String onchainDataUrl = bdmsUrl+"/v1/basis_highlevel/onchain_data/?key=doonsoft.t_neusoft_uniformity_testing.e69ba67a29094aafb0d873e7c5ac354f367"+"&ds_id="+dsId;
           // String onchainDataUrl = bdmsUrl+"/v1/basis_highlevel/onchain_data/?key=doonsoft."+tableName+"."+uniformityTesting.getDataIdentification()+"&ds_id="+dsId;
        //    String onchainDataUrl= "http://10.10.1.45/hbxy/v1/basis_highlevel/onchain_data/?key=BDMS.t_dictionary_field.1&ds_id=9133";
            String resultData = HttpUtils.get(onchainDataUrl,5000);
            dto.setOnChainData(resultData);
        }
        return dto;
    }
}